package alloy.gui;

import java.awt.Dimension;
import java.awt.Window;

import javax.help.BadIDException;
import javax.help.DefaultHelpBroker;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.swing.JOptionPane;

import alloy.util.Dbg;

/**
 * This class controls the user guide facility.  Much of the backbone was taken from 
 * the FIG source code by Roshan Gupta. 
 */
public class AlloyHelp {

        //
        // CONSTANTS
        //

        /**
         * The FIG help set.
         */
        private final static String ALLOY_HELPSET = "alloyHelp/alloy.hs";
        public final static String HELP_DEFAULT = "AlloyAnalyzerHelp";

        //
        // CLASS VARIABLES
        //
            
        /**
         * The help broker used to display Alloy's help.
         */
        private static HelpBroker helpBroker;
        static {
            try {
                HelpSet helpSet = new HelpSet(null, HelpSet.findHelpSet(null, ALLOY_HELPSET));
                helpBroker = helpSet.createHelpBroker();                
                helpBroker.initPresentation();
                helpBroker.setSize(new Dimension(800,600));
            } 
            catch (Exception e) {
                Dbg.info("Cannot find User Guide");
                helpBroker = null;
            }
        }



        //
        // METHODS
        //

        //-------------------------------------------
        /**
         * Shows the Alloy help window. If the help system was not initialized properly, shows an error dialog
         * instead.
         * 
         * @param parent
         *            the parent that owns this help dialog
         * @param helpId
         *            the help ID to display (an invalid ID will result in the top level help topic being
         *            displayed)
         */
        public static void showHelp(Window parent, String helpId) {
            if (helpBroker != null) {

                try {
                    helpBroker.setCurrentID(helpId);

                    // This is a workaround--the help window freezes when launched from a
                    // modal dialog unless we first set the activation window. However,
                    // if we set the activation window to a modal dialog, we have to
                    // re-initialize the presentation as well! Definitely a result of
                    // various bugs in JavaHelp.
                    ((DefaultHelpBroker) helpBroker).setActivationWindow(parent);
                    helpBroker.initPresentation();
                }
                catch (BadIDException bie) {
                    helpBroker.setCurrentID(HELP_DEFAULT);
                }


                // To maintain the last size of the help window, it
                // is necessary to explicitly get the size and set
                // it again. Note that the CSH class that normally
                // should be used to display the help window doesn't
                // respect the last size of the help window (a bug
                // in JavaHelp?).
                //menuItem.addActionListener(new CSH.DisplayHelpFromSource(helpBroker));
                helpBroker.setSize(helpBroker.getSize());

                helpBroker.setDisplayed(true);
            }


            else {        
                JOptionPane.showMessageDialog(parent,
                                              "Could not locate the Alloy Analyzer User Guide!",
                                              "Help Error", 
                                              JOptionPane.WARNING_MESSAGE);
            }
        }

    }